package cn.biplam.back.jda.jdbc;

import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import cn.biplam.common.client.DbResource;
import cn.biplam.common.wrapper.ErrorMessage;

/**
 * Derby数据库连接访问类<UL>
 * <LI>传入参数初始化
 * <LI>获取数据库连接
 * </UL>
 * @author 	WangSong 2009-6-14 下午12:08:04
 * @version 1.0
 */
public class JdbcConnectionDerby implements JdbcConnection {

	/**
	 * 用来初始化实例
	 * @throws ErrorMessage 
	 */
	public  static  JdbcConnectionDerby  getInstance(DbResource dbResource) throws ErrorMessage{ 
		return new JdbcConnectionDerby(dbResource); 
	}
	/**
	 * 用来初始化实例
	 * @param sIp   数据库服务ip
	 * @param sPort 端口
	 * @param sSid  服务名
	 * @param sUserName  用户名
	 * @param sPassword  密码
	 * @return JdbcConnectionOracle
	 */
	public  static  JdbcConnectionDerby  getInstance(String sIp,String sPort,String sSid,
            					String sUserName,String sPassword)  { 
		return new JdbcConnectionDerby(sIp,sPort,sSid,sUserName,sPassword); 
	}
	private DbResource dbResource;
	/**数据库密码*/
	private String sPassword;
	/**数据库实例名*/
	private String sSid;

	/**数据库连接字符串*/
	private String sUrl;
	
	/**数据库用户*/
	private String sUser;


	/**
	 *
	 * 私有化构造函数
	 * @param DbResource  数据源信息
	 * @throws ErrorMessage 
	 */
	private JdbcConnectionDerby(DbResource dbResource) throws ErrorMessage{
		this.dbResource = dbResource;
		try {
			this.sUrl = "jdbc:derby://"+this.dbResource.getIp()+":"+this.dbResource.getPort();
			this.sSid = this.dbResource.getDssid();
			this.sUser = this.dbResource.getUsername();
			this.sPassword = this.dbResource.getPassword();
		} catch (Exception e) {
			ErrorMessage mErrorMessage = new ErrorMessage(e);
			throw mErrorMessage; 
		}
	}
	
	/**
	 * 构造方法
	 * @param sIp   数据库服务ip
	 * @param sPort 端口
	 * @param sSid  服务名
	 * @param sUserName  用户名
	 * @param sPassword  密码
	 */
	private JdbcConnectionDerby(String sIp,String sPort,String sSid,
			                     String sUserName,String sPassword) {

			this.sUrl = "jdbc:derby://"+sIp+":"+sPort;
			this.sSid = sSid;
			this.sUser = sUserName;
			this.sPassword = sPassword;
	}

	/**
	 * 获取数据库连接
	 * @return Connection
	 * @throws RemoteException 
	 */
	public Connection getConnection() throws ErrorMessage{
		Connection conn = null;
		//根据DbResource信息取得数据库连接
		if(this.sUser==null || "".equals(sUser.trim())){
			conn = getConnectionAloneMode();
		}else{
			conn = getConnectionNetMode();
		}
		return conn;
	}
	
	/**
	 * 独立模式
	 * @return
	 * @throws ErrorMessage
	 */
	private Connection getConnectionNetMode() throws ErrorMessage{
		Connection conn = null;
		//根据DbResource信息取得数据库连接
		try {
			Class.forName("org.apache.derby.jdbc.ClientDriver" );
			conn = DriverManager.getConnection( sUrl+"/"+this.sSid,this.sUser,this.sPassword);
		} catch (ClassNotFoundException e) {
			ErrorMessage mErrorMessage = new ErrorMessage(e);
			throw mErrorMessage; 
		} catch (SQLException e) {
			ErrorMessage mErrorMessage = new ErrorMessage(e);
			throw mErrorMessage; 
		} 
		return conn;
	}
	
	/**
	 * 嵌入模式
	 * @return
	 * @throws ErrorMessage
	 */
	private Connection getConnectionAloneMode() throws ErrorMessage{
		Connection conn = null;
		//根据DbResource信息取得数据库连接
		try {
			String dbPath = dbResource.getIp();
			Class.forName("org.apache.derby.jdbc.EmbeddedDriver" );
			conn = DriverManager.getConnection( "jdbc:derby:"+dbPath+this.sSid+";create=true");
		} catch (ClassNotFoundException e) {
			ErrorMessage mErrorMessage = new ErrorMessage(e,"getConnectionAloneMode");
			throw mErrorMessage; 
		} catch (SQLException e) {
			ErrorMessage mErrorMessage = new ErrorMessage(e,"getConnectionAloneMode");
			throw mErrorMessage; 
		} 
		return conn;
	}
}
